﻿using UnityEditor;
using UnityEngine;

namespace EditorFrameWork
{
    public class FolderField: GUIBase
    {
        protected string mPath = "Assets";

        protected string Path
        {
            get { return mPath; }
            set
            {
                mPath = value;
                mPath = Path;
            }
        }

        public string Title;
        public string Folder;
        public string DefaultName;
        public FolderField(string path = "Assets", string folder = "Assets", string title = "Select Folder", string defaultName = "")
        {
            mPath = path;
            Title = title;
            Folder = folder;
            DefaultName = defaultName;
        }
        public override void OnGUI(Rect position)
        {
            base.OnGUI(position);
            var rects = position.VerticalSplit(position.width - 30);
            var leftRect = rects[0];
            var rightRect = rects[1];
            var currentGUIEnabled = GUI.enabled;
            GUI.enabled = false;
            EditorGUI.TextField(leftRect, mPath);;//左边 rect width - 30
            GUI.enabled = currentGUIEnabled;
            if (GUI.Button(rightRect, GUIContents.Folder))//右边 rect 30
            {
                var path = EditorUtility.OpenFolderPanel(Title, Folder, DefaultName);
                if (!string.IsNullOrEmpty(path) && path.IsDirectory())
                {
                    mPath = path.ToAssetsPath();

                }
            }

            var dragInfo = DragAndDropTool.Drag(Event.current, leftRect);
            if (dragInfo.EnterArea && dragInfo.Complete && !dragInfo.Dragging && dragInfo.Paths[0].IsDirectory())
            {
                mPath = dragInfo.Paths[0];
            }
        }

        protected override void OnDispose()
        {

        }
    }
}